//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Windows.Input;

namespace OpenLS.Media
{
    public class OfficeCursors
    {
        private static readonly Cursor paintCell;

        private static readonly Cursor fill;

        private static readonly Cursor fillIncrement;

        private static Cursor dragCell;

        private static readonly Cursor defaultCell;

        private static readonly Cursor sizeNS;

        private static readonly Cursor sizeWE;

        private static readonly Cursor arrowRight;

        private static readonly Cursor arrowDown;

        private static readonly Cursor rotate;

        private static readonly Cursor rotate4;

        static OfficeCursors()
        {
            Assembly a = Assembly.GetExecutingAssembly();
            a.GetManifestResourceStream("cursors.DefaultCell.cur");
            foreach (FieldInfo f in typeof (OfficeCursors).GetFields(BindingFlags.Static | BindingFlags.NonPublic))
            {
                using (
                    Stream stream = a.GetManifestResourceStream(typeof (OfficeCursors).FullName + "." + f.Name + ".cur")
                    )
                {
                    if (!PermissionHelper.HasCursorPermission)
                        f.SetValue(null, Cursors.Arrow);
                    else
                        f.SetValue(null, new Cursor(stream));
                }
            }
            if (!PermissionHelper.HasCursorPermission)
            {
                paintCell = Cursors.Arrow;
                fill = Cursors.ScrollS;
                arrowDown = Cursors.ScrollS;
                arrowRight = Cursors.ScrollE;
                defaultCell = Cursors.Cross;
                fillIncrement = Cursors.ScrollS;
                rotate = Cursors.Arrow;
                rotate4 = Cursors.Arrow;
                sizeNS = Cursors.SizeNS;
                sizeWE = Cursors.SizeWE;
            }
        }

        public static Cursor PaintCell
        {
            get { return paintCell; }
        }

        public static Cursor Fill
        {
            get { return fill; }
        }

        public static Cursor FillIncrement
        {
            get { return fillIncrement; }
        }

        public static Cursor DragCell
        {
            get { return dragCell; }
        }

        public static Cursor DefaultCell
        {
            get { return defaultCell; }
        }

        public static Cursor SizeNS
        {
            get { return sizeNS; }
        }

        public static Cursor SizeWE
        {
            get { return sizeWE; }
        }

        public static Cursor ArrowRight
        {
            get { return arrowRight; }
        }

        public static Cursor ArrowDown
        {
            get { return arrowDown; }
        }

        public static Cursor Rotate
        {
            get { return rotate; }
        }

        public static Cursor Rotate4
        {
            get { return rotate4; }
        }

        #region Nested type: PermissionHelper

        private static class PermissionHelper
        {
            [ThreadStatic] private static bool? _hasCursorPermission;

            internal static
                bool HasCursorPermission
            {
                get
                {
                    if (_hasCursorPermission == null)
                    {
                        try
                        {
                            new EnvironmentPermission(PermissionState.Unrestricted).Demand();
                            _hasCursorPermission = true;
                        }
                        catch (Exception)
                        {
                            _hasCursorPermission = false;
                        }
                    }
                    return _hasCursorPermission.Value;
                }
            }
        }

        #endregion
    }
}